//常用的工具类
import store from '@/store'
import router from '@/router/index'

const TOKEN='tokenObj';
/**
 * 跳转
 * @param page
 */
export function gotoPage(page,name) {
    if(page == '/login'){
        router.push('/login');
        return;
    }

    let data = {path:page,name:name}
    // if(page ==store.getters.currentShow ){
    //     console.log("------拒绝")
    //     return;
    // }
    store.commit('ADD_CURRENT_SHOW',data);

    router.push(page);
}

/**
 * 获取token --超期返回null
 */
export  function getToken(){
    let data =  getLocalStoreObj(TOKEN);
    //token不存在
    if(!data){
        return null;
    }
    let expired = data.expired;
    let current = new Date().getTime();
    if(current >= expired){
        return null;
    }
    return data.token;

}

/**
 * 设置token
 *  两个元素token 过期时间
 */
export function setToken(data){
    window.localStorage.setItem(TOKEN,JSON.stringify(data));
}

/**
 * 设置token
 *  两个元素token 过期时间
 */
export function removeToken(){
    window.localStorage.removeItem(TOKEN);
}

/**
 * 设置本地缓存
 * @param key
 * @param value
 */
export function setLocalStoreString(key,value){
    window.localStorage.setItem(key,value);
}

/**
 * 获取本地缓存
 * @param key
 * @returns {string}
 */
export function getLocalStoreString(key){
    return window.localStorage.getItem(key);
}
/**
 * 获取本地缓存
 * @param key
 * @returns {string}
 */
export function removeLocalStore(key){
    return window.localStorage.removeItem(key);
}

/**
 * 设置本地缓存对象
 * @param key
 * @param value
 */
export function setLocalStoreObj(key,value){
    window.localStorage.setItem(key,JSON.stringify(value));
}

/**
 * 获取本地缓存对象
 * @param key
 * @returns {any}
 */
export function getLocalStoreObj(key){
    let data = window.localStorage.getItem(key);
    return JSON.parse(data);
}